package com.bestcem.xm.survey.export.type;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.survey.export.BaseTypeHandler;
import com.bestcem.xm.survey.grpc.client.dto.QstructsDto;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

import static com.bestcem.xm.survey.enums.QuestionTypeEnum.QUESTION_TYPE_MULTIPLE_BLANK;

@Component
public class MultipleBlankHandler implements BaseTypeHandler {

    @Override
    public Map<String, String> getOpenColumnChangeDictV2(JSONObject data) {
        Map<String, String> res = new HashMap();
        List<JSONObject> items = (List<JSONObject>) data.getOrDefault("items", Collections.emptyList());
        items.forEach(v -> res.put(getOpenRealcolV2(data, v), getOpenReadablecolV2(data, v)));
        return res;
    }

    private String getOpenRealcolV2(JSONObject data, JSONObject item) {
        return String.format("%s__%s__open", data.getString("cid"), item.getString("oid"));
    }

    private String getOpenReadablecolV2(JSONObject data, JSONObject item) {
        return String.format("%s__%s", BaseTypeHandler.clearHtml(data.getString("title")), BaseTypeHandler.clearHtml(item.getString("title")));
    }

    private String getScoreRealcolV2(JSONObject data) {
        return String.format("%s__score", data.getString("cid"));
    }

    @Override
    public List<String> getOpenColumnListV2(JSONObject data) {
        List<String> res = new ArrayList();
        List<JSONObject> items = (List<JSONObject>) data.getOrDefault("items", Collections.emptyList());
        items.forEach(v -> res.add(getOpenRealcolV2(data, v)));
        return res;
    }

    @Override
    public Map<Object, Object> getOpenAnswerDictV2(JSONObject dataValue, Object answerValue) {
        Map<Object, Object> data = new HashMap<>();
        List<JSONObject> items = (List<JSONObject>) dataValue.getOrDefault("items", Collections.emptyList());
        if (Objects.isNull(answerValue)) {
            //np.nan 可以理解为null
            items.forEach(item -> data.put(getOpenRealcolV2(dataValue, item), null));
        } else {
            items.forEach(item -> {
                Map<String, Object> answers = (Map<String, Object>) answerValue;
                for (Map.Entry<String, Object> entry : answers.entrySet()) {
                    String gid = entry.getKey();
                    Object answer = entry.getValue();
                    if (gid.equals(item.getString("gid"))) {
                        //三叶草一般填空题答案不是数组
                        if (answer instanceof List) {
                            data.put(getOpenRealcolV2(dataValue, item), BaseTypeHandler.clearHtml((String) ((List) answer).get(0)));
                        } else if (answer instanceof String) {
                            data.put(getOpenRealcolV2(dataValue, item), BaseTypeHandler.clearHtml((String) answer));
                        } else {
                            data.put(getOpenRealcolV2(dataValue, item), null);
                        }
                        break;
                    }
                    data.putIfAbsent(getOpenRealcolV2(dataValue, item), null);
                }
            });
        }
        return data;
    }

    @Override
    public List<List<String>> getVarLabelsV2(JSONObject dataValue, boolean not01, boolean need_title) {
        List<List<String>> data = new ArrayList<>();
        List<String> var_list = new ArrayList(), val_list = new ArrayList();
        List<JSONObject> items = (List<JSONObject>) dataValue.getOrDefault("items", Collections.emptyList());
        items.forEach(item ->
                var_list.add(String.format("VARIABLE LABELS %s__%s__open '%s'.", dataValue.get("cid"), item.get("oid"), getTitles(dataValue, null, item, false))));
        data.add(var_list);
        data.add(val_list);
        return data;
    }

    @Override
    public List<String> getSpssCodeV2(JSONObject param, QstructsDto qstruct) {
        List<JSONObject> items = (List<JSONObject>) param.getOrDefault("items", Collections.emptyList());
        List<String> head_list = items.stream().map(item -> String.format("%s__%s__open", param.get("cid"), item.get("oid"))).collect(Collectors.toList());
        return head_list;
    }

    @Override
    public String getQuestionTypeValue() {
        return QUESTION_TYPE_MULTIPLE_BLANK.getValue();
    }
}
